<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <link rel="stylesheet" media="all" th:href="@{/admin/layui/css/layui.css}">
        <link rel="stylesheet" media="all" th:href="@{/admin/style/admin.css}">
    </head>
    <body>
        <div class="layui-fluid">
            <!-- 操作 -->
            <div class="layui-card">
                <div class="layui-card-body">
                    <div style="padding-bottom: 20px;">
                        <button class="layui-btn layuiadmin-btn" data-type="add">添加</button>
                        <button class="layui-btn layuiadmin-btn" data-type="expand">展开</button>
                        <button class="layui-btn layuiadmin-btn" data-type="fold">折叠</button>
                        <button class="layui-btn layuiadmin-btn" data-type="refresh">刷新</button>
                    </div>
                    <table id="lay-list" lay-filter="lay-list"></table>
                    <!-- 操作列 -->
                    <script type="text/html" id="table-operation">
                        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="addLine"><i
                                class="layui-icon layui-icon-add-1"></i>新增
                        </a>
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                                class="layui-icon layui-icon-edit"></i>编辑
                        </a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i
                                class="layui-icon layui-icon-delete"></i>删除
                        </a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="copy"><i
                                class="layui-icon layui-icon-engine"></i>拷贝
                        </a>
                    </script>
                </div>
            </div>
        </div>

        <script type="text/html" id="typeTpl">
            {{#  if(d.type === 1){ }}
            <a class="layui-btn layui-btn-xs"><i class="layui-icon"></i>目录
            </a>
            {{#  } else { }}
            <a class="layui-btn layui-btn-primary layui-btn-xs"><i class="layui-icon"></i>按钮
            </a>
            {{#  } }}
        </script>
        <script th:src="@{/admin/layui/layui.js}"></script>
        <script>
            layui.config({
                base: '/admin/' //静态资源所在路径
            }).extend({
                index: 'lib/index' //主入口模块
                , treeTable: 'treeTable'
            }).use(['index', 'admin', 'treeTable'], function () {
                var $ = layui.$, admin = layui.admin, treeTable = layui.treeTable;

                // 渲染表格
                var renderTable = treeTable.render({
                    elem: '#lay-list',
                    tree: {
                        iconIndex: 1  // 折叠图标显示在第几列
                        , isPidData: true  // 是否是pid形式数据
                        , arrowType: 'arrow2'  // 自定义箭头风格
                    },
                    cols: [
                        {type: 'numbers', width: 80, align: 'center'},
                        {field: 'name', title: '菜单名称', width: 500},
                        {field: 'icon', title: '菜单图标', align: 'center'},
                        {field: 'type', width: 100, title: '菜单类型', align: 'center', templet: '#typeTpl'},
                        {field: 'url', title: '菜单URL', align: 'center'},
                        {field: 'perm', title: '菜单权限', align: 'center'},
                        {field: 'sort', width: 100, title: '排序', align: 'center'},
                        {
                            title: '操作',
                            width: 300,
                            minWidth: 50,
                            align: 'center',
                            fixed: 'right',
                            toolbar: '#table-operation'
                        }
                    ],
                    text: {
                        none: '<div style="padding: 18px 0;">哎呀，一条数据都没有~</div>'
                    },
                    reqData: function (data, callback) {
                        admin.req({
                            url: 'list'
                            , done: function (res) {
                                callback(res.data);  // 参数是数组类型
                                renderTable.expandAll();
                            }
                        });
                    }
                });

                //监听工具条
                treeTable.on('tool(lay-list)', function (obj) {
                    var data = obj.data;
                    // 修改
                    if (obj.event === 'edit') {
                        layer.open({
                            type: 2
                            , title: '编辑'
                            , content: 'toEdit?id=' + data.id
                            , area: ['700px', '400px']
                            , btn: ['确定', '取消']
                            , yes: function (index, layero) {
                                var iframeWindow = window['layui-layer-iframe' + index]
                                    , submit = layero.find('iframe').contents().find("#LAY-submit");
                                //监听提交
                                iframeWindow.layui.form.on('submit(LAY-submit)', function (data) {
                                    //执行 Ajax 后重载
                                    admin.req({
                                        url: 'edit'
                                        , type: 'post'
                                        , data: data.field
                                        , done: function (res) {
                                            layer.close(index);
                                            //数据刷新
                                            renderTable.refresh();
                                            layer.msg(res.msg, {icon: 1});
                                        }
                                    });
                                });
                                submit.trigger('click');
                            }
                        })
                    }
                    // 删除
                    else if (obj.event === 'delete') {
                        var field = {id: data.id}
                        layer.confirm('确定删除？', {icon: 3, title: '确认删除'}, function (index) {
                            admin.req({
                                url: 'delete'
                                , data: field
                                , done: function (res) {
                                    layer.close(index);
                                    //数据刷新
                                    renderTable.refresh();
                                    layer.msg(res.msg, {icon: 1});
                                }
                            });
                        });
                    }
                    // 拷贝
                    else if (obj.event === 'copy') {
                        var field = {id: data.id}
                        layer.confirm('确定拷贝？', {icon: 3, title: '确认拷贝'}, function (index) {
                            admin.req({
                                url: 'copy'
                                , data: field
                                , done: function (res) {
                                    layer.close(index);
                                    //数据刷新
                                    renderTable.refresh();
                                    layer.msg(res.msg, {icon: 1});
                                }
                            });
                        });
                    }
                    // 行级添加
                    else if (obj.event === 'addLine') {
                        addData(data.id);
                    }
                });

                function addData(pid) {
                    layer.open({
                        type: 2
                        , title: '添加'
                        , content: 'toAdd/' + pid
                        , area: ['700px', '400px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submit = layero.find('iframe').contents().find("#LAY-submit");
                            //监听提交
                            iframeWindow.layui.form.on('submit(LAY-submit)', function (data) {
                                //执行 Ajax 后重载
                                admin.req({
                                    url: 'add'
                                    , type: 'post'
                                    , data: data.field
                                    , done: function (res) {
                                        layer.close(index);
                                        //数据刷新
                                        renderTable.refresh();
                                        layer.msg(res.msg, {icon: 1});
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                }

                // 点击事件触发
                $('.layui-btn.layuiadmin-btn').on('click', function () {
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });

                //事件
                var active = {
                    // 新增
                    add: function () {
                        addData(0);
                    }
                    // 展开
                    , expand: function () {
                        renderTable.expandAll();
                    }
                    // 折叠
                    , fold: function () {
                        renderTable.foldAll();
                    }
                    // 刷新
                    , refresh: function () {
                        renderTable.refresh();
                    }
                }
            });
        </script>
    </body>
</html>
